﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.Models;
using System.Text;
using System.Web.SessionState;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.BLL.Tbl;
using HKEA.CMS.Core.BLL.App;
namespace WebApplication.ServerAshxFile
{
    /// <summary>
    /// M_DeleteBranchOffice 的摘要说明
    /// </summary>
    public class M_DeleteBranchOffice : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            app_tblCust_BranchBLL app_branchBLL = new app_tblCust_BranchBLL();
            context.Response.ContentType = "text/plain";
            string strRet = string.Empty;
            var customerENT = BasePage.GetInstance().GetCustomerFromSession();
            if (customerENT != null)
            {
                string strKeyValue = context.Request.QueryString["UID"];
                if (!string.IsNullOrEmpty(strKeyValue))
                {
                    app_tblCust_Branch app_branch = new app_tblCust_Branch();
                    app_branch.CustomerUID = customerENT.CustomerUID;
                    string[] Info = strKeyValue.Split('-');
                    string From = Info[1];
                    int KeyUID = Convert.ToInt32(Info[0]);
                    if (From == "True")
                    {
                        app_branch.AppCustBranchUID = KeyUID;
                        app_branch.Deleted = 0;
                        app_branch = app_branchBLL.GetEntity(app_branch, true);
                        if (app_branch != null)
                        {
                            if (app_branch.CustBranchUID > 0)
                            {
                                app_branch.Deleted = 1;
                                if (app_branchBLL.UpdateENT(app_branch) > 0)
                                {
                                    strRet = "{\"Issucess\":\"1\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200011") + "\"}";
                                    WriteToLoge(app_branch.BranchName_Eng);
                                }
                                else
                                    strRet = "{\"Issucess\":\"0\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200010") + "\"}";
                            }
                            else
                            {
                                if (app_branchBLL.DeleteENT(app_branch) > 0)
                                {
                                    strRet = "{\"Issucess\":\"1\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200011") + "\"}";
                                    WriteToLoge(app_branch.BranchName_Eng);
                                }
                                else
                                    strRet = "{\"Issucess\":\"0\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200010") + "\"}";
                            }
                        }
                        else
                        { strRet = "{\"Issucess\":\"0\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200014") + "\"}"; }
                    }
                    else
                    {
                        app_branch.CustBranchUID = KeyUID;
                        var tempappENT = app_branchBLL.GetEntity(app_branch, false);
                        if (tempappENT != null)
                        {
                            if (tempappENT.Deleted == 1)
                                strRet = "{\"Issucess\":\"0\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200014") + "\"}";
                            else
                            {
                                tempappENT.Deleted = 1;
                                if (app_branchBLL.UpdateENT(tempappENT) > 0)
                                {
                                    strRet = "{\"Issucess\":\"1\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200011") + "\"}";
                                    WriteToLoge(app_branch.BranchName_Eng);
                                }
                                else
                                    strRet = "{\"Issucess\":\"0\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200010") + "\"}";
                            }
                        }
                        else
                        {
                            tblCust_Branch tbl_branch = new tblCust_Branch();
                            tblCust_BranchBLL tbl_branchBLL = new tblCust_BranchBLL();

                            tbl_branch.Deleted = 0;
                            tbl_branch.CustBranchUID = KeyUID;
                            tbl_branch.CustomerUID = customerENT.CustomerUID;
                            tbl_branch = tbl_branchBLL.GetEntity(tbl_branch);
                            if (tbl_branch != null)
                            {
                                app_branch.AddressUID = tbl_branch.AddressUID;
                                app_branch.BranchName_Chi = tbl_branch.BranchName_Chi;
                                app_branch.BranchName_Eng = tbl_branch.BranchName_Eng;
                                app_branch.ContactInfoUID = tbl_branch.ContactInfoUID;
                                app_branch.Deleted = 1;
                                app_branch.IsHQ = tbl_branch.IsHQ;
                                app_branch.NOfEmp = tbl_branch.NOfEmp;
                                app_branch.SequenceNo = tbl_branch.SequenceNo;
                                app_branch.WebSite = tbl_branch.WebSite;
                                if (app_branchBLL.InsertENT(app_branch) > 0)
                                    strRet = "{\"Issucess\":\"1\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200011") + "\"}";
                                else
                                    strRet = "{\"Issucess\":\"0\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200010") + "\"}";
                            }
                            else
                            { strRet = "{\"Issucess\":\"0\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200014") + "\"}"; }
                        }
                    }
                }
            }
            else
            { strRet = "{\"Issucess\":\"0\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200016") + "\"}"; }
            context.Response.Write(strRet);
        }

        protected void WriteToLoge(string BrandName)
        {
            tblHistory entlog = new tblHistory();
            BasePage page = new BasePage();
            tblCustomer Company = tblCustomerBLL.GetInstance().GetEntity(page.GetEnt());
            string LoginName = tblCust_WebLoginBLL.GetInstance().GetENT(page.GetMemberLoginUID()).LoginName.ToString();
            entlog.HistoryCatUID = 2;
            entlog.HistoryTypeUID = 6;
            entlog.CustomerUID = Company.CustomerUID;
            entlog.Cust_MemberUID = 0;
            entlog.ContactPersonUID = 0;
            entlog.History_Detail = LoginName + " {" + Company.Name_Eng + "}{Brand}Delete" + BrandName;
            entlog.LastModifyBy = LoginName;
            int tal = HKEA.CMS.Core.BLL.Tbl.tblHistoryBLL.GetInstance().InsertENT(entlog);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}